//
//  VCOfBannersWeb.m
//  Gift
//
//  Created by dllo on 16/4/27.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "VCOfBannersWeb.h"

@interface VCOfBannersWeb ()<UIWebViewDelegate>
@property (nonatomic, strong)UIWebView *webView;
@property (nonatomic, strong)UIActivityIndicatorView *action;

@end

@implementation VCOfBannersWeb

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self handleBackButton];
    self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    self.webView.delegate = self;
    // 自适应 webview 大小
    self.webView.scalesPageToFit = YES;
    [self.view addSubview:self.webView];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString :_stringURL]];
    [self.webView loadRequest:request];
    
}
#pragma mark - 设置返回按钮
- (void)handleBackButton{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(backButton:)];
}
#pragma mark - 点击返回
- (void)backButton:(UIBarButtonItem *)barButton{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
#pragma mark - 开始的时候给他加载的效果
- (void)webViewDidStartLoad:(UIWebView *)webView {
    UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
    [view setTag:108];
    [view setBackgroundColor:[UIColor lightGrayColor]];
    [view setAlpha:0.5];
    [self.view addSubview:view];
    self.action = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];// 指定进度轮大小
    [self.action setCenter:view.center];// 指定进度轮中心点
    [self.action setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];// 设置进度轮显示类型
    [view addSubview:self.action];
    [self.action startAnimating];
}

// 结束
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.action stopAnimating];
    UIView *view = (UIView *)[self.view viewWithTag:108];
    [view removeFromSuperview];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [self.action stopAnimating];
    UIView *view = [self.view viewWithTag:108];
    UILabel *label = [[UILabel alloc] init];
    label.textAlignment = NSTextAlignmentCenter;
    label.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
    label.text = @"此网站无效";
    label.center = self.view.center;
    [view addSubview:label];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
